package Client.GUI;

import Client.Net.*;
import Library.*;

/**
 *
 */
public class pnlCreateGame extends javax.swing.JPanel {
    protected ICommunication m_Communication;
    
    /**
     * Creates new form pnlCreateGame
     * @param communication reference to the clientservice
     */
    public pnlCreateGame(ICommunication communication)
    {
        initComponents();
        m_Communication = communication;
        setSize(getPreferredSize());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtGameTime = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnCreate = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        txtGameName = new javax.swing.JTextField();
        txtOpmerking = new javax.swing.JLabel();
        cboMap = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        lblGameTime = new javax.swing.JLabel();
        txtArmySize = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtRoundTime = new javax.swing.JTextField();
        numAantal = new javax.swing.JSpinner();

        setPreferredSize(new java.awt.Dimension(450, 360));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtGameTime.setText("30");
        txtGameTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGameTimeActionPerformed(evt);
            }
        });
        jPanel1.add(txtGameTime, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 78, 107, -1));

        jLabel1.setText("Game name");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jLabel2.setText("Game Duration (mins)");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, 10));

        btnCreate.setText("Create");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });
        jPanel1.add(btnCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 210, 105, -1));

        jLabel3.setText("Number of players");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 154, -1, 20));

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 210, 95, -1));

        txtGameName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGameNameActionPerformed(evt);
            }
        });
        jPanel1.add(txtGameName, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 31, 170, -1));
        jPanel1.add(txtOpmerking, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, 380, 39));

        cboMap.setModel(
            new javax.swing.DefaultComboBoxModel(new String[]
                {
                    "Wereldkaart"
                }
            )
        );
        jPanel1.add(cboMap, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 127, 170, -1));

        jLabel4.setText("Map:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 107, -1, -1));

        lblGameTime.setText("Min. initial armies per Player:");
        jPanel1.add(lblGameTime, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 10, -1, -1));

        txtArmySize.setEditable(false);
        txtArmySize.setText("35");
        jPanel1.add(txtArmySize, new org.netbeans.lib.awtextra.AbsoluteConstraints(246, 31, 70, -1));

        jLabel6.setText("Maximum Turn Time (s):");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(246, 58, -1, -1));

        txtRoundTime.setText("120");
        jPanel1.add(txtRoundTime, new org.netbeans.lib.awtextra.AbsoluteConstraints(246, 78, 70, -1));

        numAantal.setFont(new java.awt.Font("Tahoma", 0, 12));
        numAantal.setModel(new javax.swing.SpinnerNumberModel(3, 2, 6, 1));
        numAantal.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                numAantalStateChanged(evt);
            }
        });
        numAantal.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                numAantalPropertyChange(evt);
            }
        });
        jPanel1.add(numAantal, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 174, 76, -1));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 420, 330));
    }// </editor-fold>//GEN-END:initComponents

    private void txtGameTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGameTimeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtGameTimeActionPerformed

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        try
        {
         // int maxPlayers = Integer.parseInt(txtMaxPlayers.getText());
          int totalTime = Integer.parseInt(txtGameTime.getText());
          int turnTime = Integer.parseInt(txtRoundTime.getText());
          int armySize = Integer.parseInt(txtArmySize.getText());
          int maxPlayers = ((Integer)numAantal.getValue()).intValue();
          // Nieuwe Game aanmaken
          System.out.println("game init " + totalTime +" "+ turnTime +" "+ armySize +" "+ maxPlayers);
          int result = m_Communication.getService().createGame(txtGameName.getText(), maxPlayers, cboMap.getSelectedIndex() + 1, totalTime, turnTime, armySize);
          if (result != ErrorCodes.ERROR_NOERROR)
          {
              m_Communication.log("WARNING: kan geen nieuwe game Cannot Create Game. Reason: " + ErrorCodes.getMessage(result));
              txtOpmerking.setText("Create game failed. Reason: " + ErrorCodes.getMessage(result) + "\r\n");
          }
          else
          { 
              txtOpmerking.setText("Game created. " + ErrorCodes.getMessage(result) + "\r\n");
              pnlLobby lobby = new pnlLobby(m_Communication, true);
              m_Communication.SetContent(lobby);
              System.out.println("You are connected to game: " + m_Communication.getService().getGame().getName());
          }
        }
        catch (Exception ex)
        {
            m_Communication.logException(ex);
        }
}//GEN-LAST:event_btnCreateActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        m_Communication.SetContent(new pnlGameList(m_Communication));
}//GEN-LAST:event_btnCancelActionPerformed

    private void txtGameNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGameNameActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtGameNameActionPerformed

    private void numAantalPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_numAantalPropertyChange
        // TODO add your handling code here:
       
    }//GEN-LAST:event_numAantalPropertyChange

    private void numAantalStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_numAantalStateChanged
        // TODO add your handling code here:
        int Players = ((Integer)numAantal.getValue()).intValue();
        if (Players == 2 )
                txtArmySize.setText("40");
        if (Players == 3 )
                txtArmySize.setText("35");
        if (Players == 4 )
                txtArmySize.setText("30");
        if (Players == 5 )
                txtArmySize.setText("25");
        if (Players == 6 )
                txtArmySize.setText("20");
 System.out.println("setplayers");
    }//GEN-LAST:event_numAantalStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.JComboBox cboMap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblGameTime;
    private javax.swing.JSpinner numAantal;
    private javax.swing.JTextField txtArmySize;
    private javax.swing.JTextField txtGameName;
    private javax.swing.JTextField txtGameTime;
    private javax.swing.JLabel txtOpmerking;
    private javax.swing.JTextField txtRoundTime;
    // End of variables declaration//GEN-END:variables

}
